package code.tools.commponents
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 *横向tab，icon样式的 
	 * @author liangqian
	 * 
	 */	
	public class HTabWithIcon extends Component
	{
		/** 特殊tab标签，all */
		public static const TAB_ICON_ALL_ID:int = 1000;
		/** 特殊tab标签，已下订单 */
		public static const TAB_ICON_ORDER_ID:int = 2000;
		
		private var _isTipsIcon:Boolean;
		private var tabItems:Array;
		/**
		 * Tabs 发生改变时候的外部注入钩子onChange(i)
		 */
		private var onChange:Function;
		/**
		 * 当前选中的 tab
		 */
		private var _selectedIndex:int = -1;
		private var _getUnlockTaskNameHandler:Function;
		private var _hListSortCallHandler:Function;
		private var _shopId:int;
		private var _area:Sprite;
		public function HTabWithIcon(area:Sprite,shopId:int,isTipsIcon:Boolean,changeHandler:Function, getUnlockTaskNameHandler:Function,hListSortCallHandler:Function,... labels)
		{
			_area = area;
			_shopId = shopId;
			_isTipsIcon = isTipsIcon;
			onChange = changeHandler;
			_getUnlockTaskNameHandler = getUnlockTaskNameHandler;
			_hListSortCallHandler = hListSortCallHandler;
			tabItems = [];
			dataprovider = labels;
		}
		
		/**
		 * 
		 * @param labels [shopItemdata]
		 * 
		 */		
		public function set dataprovider(labels:Array):void
		{
			clear();
			var i:int;
			var len:int = labels.length;
			var tabItem:TabItem;
			var xpos:int = 0;
			for(; i < len; i++)
			{
				tabItem = new TabItem(_area,labels[i],_isTipsIcon,tabChangeHandler,_getUnlockTaskNameHandler,_hListSortCallHandler,refreshSelectedIndex,_shopId);
				tabItems.push(tabItem);
				tabItem.tag = i;
				tabItem.x = xpos;
				xpos += (tabItem.width + 4);
			}
		}
		
		public function clear():void
		{
			var len:int = tabItems.length;
			var obj:TabItem;
			for (var i:int ; i < len ; i++)
			{
				obj = tabItems.pop();
				if(obj)
					_area.removeChild(obj);
			}
		}
		public function tabChangeHandler(index:int):void
		{
			_isRefesh = true;
			selectedIndex = index;
			/* 调用整个tabas的外部处 理函数 */
			onChange(_selectedIndex);
		}
		
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		
		/**
		 * 通过指证设置当前选定的 tab
		 * @param index 要选取的tab的指针
		 */
		public function set selectedIndex(index:int):void
		{
			var len:int = tabItems.length;
			var _index:int = Math.max(index,0);
			if (len == 0)
				return;
			for (var i:int ; i < len ; i++)
			{
				if(tabItems[i])
				{
					tabItems[i].selected = false;
					if(i != _index || _isRefesh)
						tabItems[i].clearHListSelected();
				}
			}
			_selectedIndex = _index;
			tabItems[_index].selected = true;
		}
		
		private var _isRefesh:Boolean;
		private function refreshSelectedIndex(index:int):void
		{
			_isRefesh = false;
			selectedIndex = index;
		}
	}
}

import code.main.AppCommonView;
import code.structure.manager.loader.DllLoaderManager;
import code.structure.manager.font.FontManager;
import code.structure.manager.language.LanguageManager;
import code.structure.manager.tips.BaseTips;
import code.structure.manager.tips.TipsFactoray;
import code.structure.module.config.data.ShopItemData;
import code.tools.commponents.Component;
import code.tools.commponents.HList;
import code.tools.commponents.HTabWithIcon;
import code.tools.commponents.Helpers;
import code.untils.FiltersEnum;
import code.untils.UiNameConst;

import com.greensock.TweenLite;
import com.patience.mvc.context.ContextIntance;

import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;

class TabItem extends Component
{
	private var tabMc:MovieClip;
	private var _tipsIconArea:Sprite;
	private var _tipsIcon:Sprite;
	private var _onlyTextTips:BaseTips;
	private var _lockIcon:Sprite;
	private var _onClick:Function;
	private var _getUnlockTaskNameHandler:Function;
	private var _isAll:Boolean;
	private var _allTf:TextField;
	private var _over:Boolean;
	
	/** 排序选项列表 */
	private var _hListSort:HList;
	private var _hListSortSortCallHanler:Function;
	private var _selectedIndex:Function;
	private var _area:Sprite;
	private var _shopId:int;
	public function TabItem(parent:DisplayObjectContainer ,
							shopItemData:*,isTipsIcon:Boolean,
							onClick:Function,getUnlockTaskNameHandler:Function,hListSortSortCallHanler:Function,selectedIndex:Function,shopId:int)
	{
		_hListSortSortCallHanler = hListSortSortCallHanler;
		_getUnlockTaskNameHandler = getUnlockTaskNameHandler;
		_selectedIndex = selectedIndex;
		_shopId = shopId;
		super(parent);
		_onClick = onClick;
		_onlyTextTips = TipsFactoray.getTips(TipsFactoray.TIPS_TYPE_ONLY_TEXT_WITH_ALERT_LAYER,(ContextIntance.getContext().commonView as AppCommonView).appContainer.popupTipsLayer);
		setTabIcon(shopItemData,isTipsIcon);
		_area.addEventListener(MouseEvent.MOUSE_OVER,hoverHandler);
	}
	
	override protected function init():void
	{
		setSize(57,52);
		super.init();
	}
	
	override protected function addChildren():void
	{
		super.addChildren();
		
		_area = new Sprite();
		addChild(_area);
		_area.mouseChildren = false;
		
		tabMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.SHOP_ORDER_VIEW + '_' + _shopId,'tab') as Class) as MovieClip;
		_area.addChild(tabMc);
		
		_tipsIconArea = new Sprite();
		_area.addChild(_tipsIconArea);
		
		_lockIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'lock') as Class) as Sprite;
		_lockIcon.scaleX = _lockIcon.scaleY = .5;
		_lockIcon.visible = false;
		_lockIcon.x = width - _lockIcon.width - 5;
		_lockIcon.y = height - _lockIcon.height - 3;
		
		/* 排序选项列表 */
		_hListSort = new HList(this,-3,54,3,
			[LanguageManager.getLanguageInfo('DESC_SORT_TIME_DESCENDING_ORDER'),
				LanguageManager.getLanguageInfo('DESC_SORT_TIME_ASCENDING_ORDER'),
				LanguageManager.getLanguageInfo('DESC_SORT_PRICE_ASCENDING_ORDER')],
			[HList.STYLE_ARROW_TOP,HList.STYLE_ARROW_DOWN,HList.STYLE_ARROW_TOP],hListSortSortHanler);
		_hListSort.visible = false;
		_hListSort.addEventListener(MouseEvent.ROLL_OVER,hListSortHoverHandler);
		_hListSort.addEventListener(MouseEvent.ROLL_OUT,hListSortOutHandler);
	}
	
	///////////////////////////////////
	// public methods
	///////////////////////////////////
	private var _selected:Boolean;
	public function set selected(value:Boolean):void
	{
		_selected = value;
		if (_selected)
		{
			tabMc.gotoAndStop(3);
			if(_isAll)
				_allTf.textColor = 0x317BaF;
			_tipsIconArea.scaleX = _tipsIconArea.scaleY = 1;
		}
		else
		{
			tabMc.gotoAndStop(_lockIcon.visible ? 4 : 1);
			if(_isAll)
				_allTf.textColor = 0xC84385;
			if(_over)
				_tipsIconArea.scaleX = _tipsIconArea.scaleY = 1.2;
			else
				_tipsIconArea.scaleX = _tipsIconArea.scaleY = 1;
		}
	}
	/**
	 * 清除掉排行的选中状态 
	 * 
	 */	
	public function clearHListSelected():void
	{
		_hListSort.clearSelectedStatus();
	}
	
	private var _nameStr:String = '';
	private var _descStr:String = '';
	private var _shopItemData:ShopItemData;
	public function setTabIcon(shopItemData:*,isTipsIcon:Boolean):void
	{
		_lockIcon.visible = false;
		_isAll = false;
		_area.buttonMode = false;
		if(shopItemData == HTabWithIcon.TAB_ICON_ALL_ID)//需替换文字的
		{
			_isAll = true;
			_area.buttonMode = true;
			_nameStr = LanguageManager.getLanguageInfo('MARKET_SUB_SECTION_1000');
			DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'tab_all',iconLoadCompleteHandler2);
			_area.addEventListener(MouseEvent.CLICK , tabItemClickHandler);
		}
		else if(shopItemData == HTabWithIcon.TAB_ICON_ORDER_ID)//已下订单
		{
			_area.buttonMode = true;
			DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'tab_order_dessert',iconLoadCompleteHandler3);
			_area.addEventListener(MouseEvent.CLICK , tabItemClickHandler);
			_nameStr = LanguageManager.getLanguageInfo('TIPS_TAB_ORDER');
		}
		else
		{
			_shopItemData = shopItemData as ShopItemData;
			if(_shopItemData.iUnlock != 0 && _shopItemData.iUnlock != 1)
			{
				_lockIcon.visible = true;
				_area.removeEventListener(MouseEvent.CLICK , tabItemClickHandler);
				if(_shopItemData.iUnlock == 2)
					_descStr = LanguageManager.getLanguageInfo('TIPS_MARKET_MISSION_UNLOCK',[_getUnlockTaskNameHandler(_shopItemData.iTaskID)]);
				else
					_descStr = LanguageManager.getLanguageInfo('TIPS_MARKET_LEVEL_UNLOCK',[_shopItemData.iLevel]);
			}
			else
			{
				_lockIcon.visible = false;
				_area.buttonMode = true;
				_area.addEventListener(MouseEvent.CLICK , tabItemClickHandler);
				_descStr = '';
			}
			_nameStr = shopItemData.sName;
//			if(isTipsIcon)
//				DllLoaderManager.getInstance().getMaterial(shopItemData.sTipsIcon,'ICON_TIP',iconLoadCompleteHandler,shopItemData.sTipsFileType);
//			else
				DllLoaderManager.getInstance().getMaterial(shopItemData.sPath,'ICON',iconLoadCompleteHandler,shopItemData.fileType);
		}
	}
	
	private function iconLoadCompleteHandler2(c:*,key:String = ''):void
	{
		clearIcon();
		_tipsIcon = Helpers.drawBmd(c,_tipsIcon);
		_tipsIcon.x = -_tipsIcon.width*.5;
		_tipsIcon.y = -_tipsIcon.height*.5;
		_allTf = Helpers.processTf(_tipsIcon,'all_text',LanguageManager.getLanguageInfo('MARKET_SUB_SECTION_1000'),FontManager.getInstance().embeddedFontBName,true,true);
		_tipsIconArea.addChild(_tipsIcon);
		_tipsIconArea.x = _tipsIcon.width*.5 + (width - _tipsIcon.width)*.5 - 2;
		_tipsIconArea.y = _tipsIcon.height*.5 + height - _tipsIcon.height - 3;
		selected = _selected;
	}
	
	private function iconLoadCompleteHandler(c:*,key:String = ''):void
	{
		clearIcon();
		_tipsIcon = Helpers.drawBmd(c,_tipsIcon);
		_tipsIcon.filters = [FiltersEnum.CHARTS_RECIPE_ICON_DROPSHADOWFILTER];
		_tipsIcon.scaleX = _tipsIcon.scaleY = .5;
		_tipsIcon.x = -_tipsIcon.width*.5;
		_tipsIcon.y = -_tipsIcon.height*.5 - 3;
		_tipsIconArea.addChild(_tipsIcon);
		_tipsIconArea.x = _tipsIcon.width*.5 + (width - _tipsIcon.width)*.5;
		_tipsIconArea.y = _tipsIcon.height*.5 + (height - _tipsIcon.height)*.5;
		if(!_lockIcon.visible)
			selected = _selected;
		else
		{
			_area.addChild(_lockIcon);
			selected = false;
		}
	}
	
	private function iconLoadCompleteHandler3(c:*,key:String = ''):void
	{
		clearIcon();
		_tipsIcon = Helpers.drawBmd(c,_tipsIcon);
		_tipsIcon.x = -_tipsIcon.width*.5;
		_tipsIcon.y = -_tipsIcon.height*.5;
		_tipsIconArea.addChild(_tipsIcon);
		_tipsIconArea.x = _tipsIcon.width*.5 + (width - _tipsIcon.width)*.5;
		_tipsIconArea.y = _tipsIcon.height*.5 + (height - _tipsIcon.height)*.5;
		if(!_lockIcon.visible)
			selected = _selected;
		else
		{
			_area.addChild(_lockIcon);
			selected = false;
		}
	}
	
	private function clearIcon():void
	{
		if(_tipsIcon)
		{
			_tipsIcon.parent.removeChild(_tipsIcon);
			_tipsIcon = null;
		}
	}
	
	private function hoverHandler(e:MouseEvent):void
	{
		_over = true;
		_area.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
		var p:Point = this.localToGlobal(new Point());
		_onlyTextTips.show(this,_nameStr + '\n' + _descStr,p.x,p.y + 6);
		if(_lockIcon.visible)return;//锁定的不放大
		selected = _selected;
		_hListSort.visible = true;
		
	}
	private function outHandler(e:MouseEvent):void
	{
		_over = false;
		_area.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
		_onlyTextTips.close();
		if(_lockIcon.visible)return;//锁定的不放大
		selected = _selected;
		TweenLite.to(_hListSort,.1,{onComplete:completeHandler});
	}
	
	private function hListSortSortHanler(index:int):void
	{
		_selectedIndex(tag);
		_hListSortSortCallHanler(tag,index);
		_hListSort.visible = false;
	}
	
	private function tabItemClickHandler(e:MouseEvent):void
	{
		_onClick(tag);
	}
	
	private function hListSortHoverHandler(e:MouseEvent):void
	{
		_hListSortOver = true;
	}
	
	private function hListSortOutHandler(e:MouseEvent):void
	{
		_hListSortOver = false;
		_hListSort.visible = false;
	}
	
	private function completeHandler():void
	{
		if(_hListSortOver)return;
		_hListSort.visible = false;
	}
	
	private var _hListSortOver:Boolean;
}
